// 仿照 noop() 实现本地存储

exports.MyLocalStorage = function () {
	let tempArr = JSON.parse(localStorage.getItem('test_temp_arr')) || []
	return {
		create: function (annotation) {
			if (typeof annotation.id === 'undefined' ||
				annotation.id === null) {
				annotation.id = uuidv4() //=> "V1StGXR8_Z5jdHi6B-myT"
			}

			console.log('┏ create -- MyLocalStorage ----------------------------- ')
			console.log('┃ annotation', annotation)
			console.log('┗ ------------------------------------')

			tempArr.push(annotation)
			localStorage.setItem('test_temp_arr', JSON.stringify(tempArr))
			return annotation;
		},

		update: function (annotation) {
			console.log('┏ update -- MyLocalStorage ----------------------------- ')
			console.log('┃ annotation', annotation)
			console.log('┗ ------------------------------------')
			// 根据id 更新缓存
			const index = tempArr.findIndex(item => item.id === annotation.id)
			tempArr[index] = annotation
			localStorage.setItem('test_temp_arr', JSON.stringify(tempArr))
			return annotation;
		},

		'delete': function (annotation) {
			console.log('┏ delete -- MyLocalStorage ----------------------------- ')
			console.log('┃ annotation', annotation)
			console.log('┗ ------------------------------------')
			// 根据id 删除缓存
			tempArr = tempArr.filter(item => item.id !== annotation.id)
			localStorage.setItem('test_temp_arr', JSON.stringify(tempArr))
			return annotation;
		},

		query: function (queryObj) {
			return {results:tempArr};
		},

		configure: function (registry) {
			registry.registerUtility(this, 'storage');
		}
	};
};

// id returns an identifier unique within this session
var id = (function () {
	var counter;
	counter = -1;
	return function () {
		return counter += 1;
	};
}());
